﻿using DataAccess.DTO;
using DataAccess.Models;

using DataAccess.Solr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Agent.Controllers
{
    public class ProductController : AgentBaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cid"></param>
        /// <param name="s">排序</param>
        /// <returns></returns>
        //public ActionResult List(int?id,int?cid,string s)
        //{
        //    string Html = "<li><a href=\"{1}/sc.html?s=1{0}\">默认排序</a></li> <li><a href=\"{1}/sc.html?s=2{0}\">销量最高</a></li><li><a href=\"{1}/sc.html?s=3{0}\">价格最低</a></li>";
        //    string OrderHtml = "<a href=\"{5}/sc.html?s=1{0}\" class=\"swiper-slide\"><span {1}>默认</span></a><a href=\"{5}/sc.html?s=2{0}\" class=\"swiper-slide\"><span>销量</span></a><a href=\"{5}/sc.html?s=3{0}\" class=\"swiper-slide\"><span>价格</span></a><a href=\"{5}/sc.html?s=4{0}\" class=\"swiper-slide\"><span>人气</span></a>";
             
        //    if (id.HasValue && id == 9)
        //    {
        //        ViewBag.Title = "9.9专区";
        //        ViewBag.type = "1";
        //        ViewBag.Url = "/" + aid + "/9.9.html";
        //        Html = "<li><a href=\"{1}/9.9.html?s=1{0}\">默认排序</a></li> <li><a href=\"{1}/9.9.html?s=2{0}\">销量最高</a></li><li><a href=\"{1}/9.9.html?s=3{0}\">价格最低</a></li>";
        //        OrderHtml = "<a href=\"{5}/9.9.html?s=1{0}\" class=\"swiper-slide\"><span {1}>默认</span></a><a href=\"{5}/9.9.html?s=2{0}\" class=\"swiper-slide\"><span {2}>销量</span></a><a href=\"/9.9.html?s=3{0}\" class=\"swiper-slide\"><span {3}>价格</span></a><a href=\"{5}/9.9.html?s=4{0}\" class=\"swiper-slide\"><span {4}>人气</span></a>";
             
        //    }
        //    if (id.HasValue && id == 19)
        //    {
        //        ViewBag.Title = "19.9专区";
        //        ViewBag.type = "3";
        //        ViewBag.Url = "/" + aid + "/19.9.html";
        //        Html = "<li><a href=\"{1}/19.9.html?s=1{0}\">默认排序</a></li> <li><a href=\"{1}/19.9.html?s=2{0}\">销量最高</a></li><li><a href=\"{1}/19.9.html?&s=3{0}\">价格最低</a></li>";
        //        OrderHtml = "<a href=\"{5}/19.9.html?s=1{0}\" class=\"swiper-slide\"><span {1}>默认</span></a><a href=\"{5}/19.9.html?s=2{0}\" class=\"swiper-slide\"><span {2}>销量</span></a><a href=\"{5}/19.9.html?s=3{0}\" class=\"swiper-slide\"><span {3}>价格</span></a><a href=\"{5}/19.9.html?s=4{0}\" class=\"swiper-slide\"><span {4}>人气</span></a>";
             
        //    }
        //    string Key = XYD.Common.Request.GetQueryString("Key");
        //    if (XYD.Common.Request.GetQueryString("Key").Trim() != "")//搜索
        //    {
        //        ViewBag.Title = Key;
        //        ViewBag.Key = Key;
        //        ViewBag.type = "4";
        //        ViewBag.Url = "/" + aid + "/sc.html";
        //        Html = "<li><a href=\"{1}/sc.html?key=" + Key + "&s=1{0}\">默认排序</a></li> <li><a href=\"{1}/sc.html?key=" + Key + "&s=2{0}\">销量最高</a></li><li><a href=\"{1}/sc.html?key=" + Key + "&s=3{0}\">价格最低</a></li>";
        //        OrderHtml = "<a href=\"{5}/sc.html?key=" + Key + "&s=1{0}\" class=\"swiper-slide\"><span {1}>默认</span></a><a href=\"{5}/sc.html?key=" + Key + "&s=2{0}\" class=\"swiper-slide\"><span {2}>销量</span></a><a href=\"{5}/sc.html?key=" + Key + "&s=3{0}\" class=\"swiper-slide\"><span {3}>价格</span></a><a href=\"{5}/sc.html?key=" + Key + "&s=4{0}\" class=\"swiper-slide\"><span {4}>人气</span></a>";
             
        //    }
        //    ViewBag.sort = s;//排序
           
            
        //    if (cid.HasValue&&cid!=0)
        //    {
        //        ViewBag.Url = "/"+aid+"/c_"+cid+".html";
        //        Html = "<li><a href=\"{1}/c_" + cid + ".html?s=1{0}\">默认排序</a></li> <li><a href=\"{1}/c_" + cid + ".html?s=2{0}\">销量最高</a></li><li><a href=\"{1}/c_" + cid + ".html?s=3{0}\">价格最低</a></li>";
        //        OrderHtml = "<a href=\"{5}/c_" + cid + ".html?s=1{0}\" class=\"swiper-slide\"><span {1}>默认</span></a><a href=\"{5}/c_" + cid + ".html?s=2{0}\" class=\"swiper-slide\"><span {2}>销量</span></a><a href=\"{5}/c_" + cid + ".html?s=3{0}\" class=\"swiper-slide\"><span {3}>价格</span></a><a href=\"{5}/c_" + cid + ".html?s=4{0}\" class=\"swiper-slide\"><span {4}>人气</span></a>";
             
        //        var model= ProductClassList.ClassList.Find(c => c.ID == cid);
        //       ViewBag.type = "2";
        //        if(model!=null)
        //            ViewBag.Title = model.Name;
        //    }
        //    ViewBag.classid = cid.HasValue?cid.ToString():"0";
        //    string strprice = XYD.Common.Request.GetQueryString("strprice");
        //    string endprice = XYD.Common.Request.GetQueryString("endprice");
        //    if (strprice != "" || strprice != "")
        //    {
        //        Html = string.Format(Html, "&strprice=" + strprice + "&endprice=" + endprice,"/"+aid);
        //        OrderHtml = string.Format(OrderHtml, "&strprice=" + strprice + "&endprice=" + endprice,(s == "1" ? "class='cur'" : ""), (s == "2" ? "class='cur'" :""),( s == "3" ? "class='cur'" : ""), (s == "4" ? "class='cur'":""),"/"+aid); 
        //    }

        //    else { Html = string.Format(Html, "", "/" + aid);
        //    OrderHtml = string.Format(OrderHtml, "", (s == "1" ? "class='cur'" : ""), (s == "2" ? "class='cur'" : ""), (s == "3" ? "class='cur'" : ""), (s == "4" ? "class='cur'" : ""), "/" + aid);
        //    }
        //    ViewBag.OrderHtml = OrderHtml;
        //    ViewBag.Html = Html;
        //    ViewBag.strprice = strprice; //价格
        //    ViewBag.endprice = endprice; //价格

        //    return View();
        //}
        public ActionResult Goods()
        {
            SolrIndexInfoBLL bll = new SolrIndexInfoBLL(); 
            int offset = XYD.Common.Request.GetFormInt("limit", 1);
            int classid = XYD.Common.Request.GetFormInt("cid", 0);
            int type = XYD.Common.Request.GetFormInt("type", 0);
            int sort = XYD.Common.Request.GetFormInt("sort", 0);
            string Key = XYD.Common.Request.GetFormString("Key");
            string strwhere = "(biz30day:[0 TO *])";
            if (classid != 0)//按分类查找 
            {
                strwhere += string.Format("AND(DataTypeID:{0})", classid);
            }
            if (type == 1)//9.9
            {
                strwhere = strwhere + " AND(prices:[0 TO 10])";
            }
            if (type == 3)//19.9
            {
                strwhere = strwhere + " AND(prices:[10 TO 20])";
            }
             
             int strprice = XYD.Common.Request.GetFormInt("strprice",0);
             int endprice = XYD.Common.Request.GetFormInt("endprice", 0);
             if (strprice != 0 || endprice != 0)
             {
                 if (strprice != 0 && endprice != 0)
                 {
                     strwhere = strwhere + " AND(prices:[" + strprice + " TO " + endprice + "])";
                 }
                 else
                 {
                     if(strprice!=0)
                         strwhere = strwhere + " AND(prices:[" + strprice + " TO *])";
                     else
                         strwhere = strwhere + " AND(prices:[0 TO " + endprice + "])";
                 }

             }
             if (Key!="")//s搜索
            {
               
                if (!string.IsNullOrEmpty(Key))
                {
                    strwhere += "AND(";
                    strwhere += string.Format("(productname:{0})", Key);
                    strwhere += ")";
                }
            }
            string Order = "biz30day desc";
            if (sort != 0)
            {
                switch (sort)
                {
                    case 1:
                        Order = "synchro_date desc";
                        break;
                    case 2:
                        Order = "biz30day desc";
                        break;
                    case 3:
                        Order = "prices asc";
                        break;
                    default:
                        break;
                }
            }
            offset = offset > 1 ? (offset - 1) * 10 + 1 : 0;

            PagingList<ProductIndex> plist = bll.GetProductList(strwhere, offset, 10, Order);
            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            return Json(new { State = true, Message = "查询成功", total = plist.Total, rows = plist });
        }
        /// <summary>
        /// 在浏览器中打开
        /// </summary>
        /// <returns></returns>
        public ActionResult OpenTao(string link)
        {
            ViewBag.link = link;
            return View();
        }
        //分类
        public ActionResult List2() 
        {
            return View();   
        }

        //收藏
        public ActionResult CollectList()
        {
           List<string> list=CartOperate.Read();
           List<ProductIndex> lists = new List<ProductIndex>();
           if (list != null && list.Count>0)
           {
               string str = "";
               foreach (var item in list)
               {
                   str = str + " id:"+item+" OR";
               }
               if (str != "")
                   str = "("+str.Substring(0, str.Length - 2)+")";
                   
               
              lists= new SolrIndexInfoBLL().GetProductDetailList(str);
              
           }
           return View(lists);
        }
        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <returns></returns>
        public ActionResult CollectAdd(string commodityID)
        {
            if(!string.IsNullOrEmpty(commodityID))
            {
                CartOperate.AddCart(commodityID);
            }
            return Json(new { statusCode = 200, Message = "成功" });
        }
        /// <summary>
        /// 商品详情
        /// </summary>
        /// <returns></returns>
        public ActionResult Detail(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                ProductIndex model = new SolrIndexInfoBLL().GetProductIndexByProductInfo(id);
                if (model!=null)
                    return View(model);
            }
            return Redirect("/");

        }

        public ActionResult Search()
        {
            return View();
        }

	}
}